package fourmiefight.ihm;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JSlider;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.ListSelectionModel;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;

import fourmiefight.metier.Carte;
import fourmiefight.metier.Fourmiliere;

public class ParamView implements ActionListener 
{
	// Declaration des constantes
	public static String TITRE = "Paramètres"; 
	
	public static String NOM = "Nom : "; 
	public static String PATHFINDING = "Pathfinding"; 

	public static String PHEROMONES = "Phéromones";
	public static String PORTEE = "Portée : ";
	public static String PERSISTANCE = "Persitance : "; 
	
	public static String FOURMIS = "Fourmis"; 
	public static String TAILLE_FOURMILLIERE = "Taille de la fourmilliere : "; 
	public static String DUREE_VIE = "Durée de vie : "; 
	public static String NB_DEPART = "Nombre de départ : "; 
	public static String FREQUENCE_PONTE = "Fréquence de ponte : "; 
	public static String NOURRITURE_DEPART = "Nourriture de départ : "; 

	public static String BOUTON_APPLIQUER = "Appliquer"; 
	public static String BOUTON_ANNULER = "Annuler";
	
	// Commposants graphiques
	private JFrame fenParam = new JFrame();
	
	// - JLabel
	private JLabel jlTitre = new JLabel(TITRE);
	private JLabel jlCenterGeneralNom = new JLabel(NOM);
	private JLabel jlCenterGeneralPathFinding = new JLabel(PATHFINDING);
	
	private JLabel jlCenterPheromonesTitre = new JLabel(PHEROMONES);
	private JLabel jlCenterPheromonesPortee = new JLabel(PORTEE);
	private JLabel jlCenterPheromonesPersistance = new JLabel(PERSISTANCE);
	
	private JLabel jlCenterFourmisTitre = new JLabel(FOURMIS);
	private JLabel jlCenterFourmisTailleFourmillieres = new JLabel(TAILLE_FOURMILLIERE);
	private JLabel jlCenterFourmisDureeVie = new JLabel(DUREE_VIE);
	private JLabel jlCenterFourmisNbDepart = new JLabel(NB_DEPART);
	private JLabel jlCenterFourmisFrequencePonte = new JLabel(FREQUENCE_PONTE);
	private JLabel jlCenterFourmisNourritureDepart = new JLabel(NOURRITURE_DEPART);
	
	// - JButton
	private JButton jbAppliquer = new JButton(BOUTON_APPLIQUER);
	private JButton jbAnnuler = new JButton(BOUTON_ANNULER);
	
	// - JTextfield
	private JTextField jtNom = new JTextField(20);
	
	private JTextField jtPheromonePortee = new JTextField(5);
	private JTextField jtPheromonePersistance = new JTextField(5);
	private JTextField jtFourmisTailleFourmiliere = new JTextField(5);
	private JTextField jtFourmisDureeVie = new JTextField(5);
	private JTextField jtFourmisNbDepart = new JTextField(5);
	private JTextField jtFourmisFrequencePonte = new JTextField(5);
	private JTextField jtFourmisNourritureDepart = new JTextField(5);
	
	// - JComboBox
	private JComboBox jcPathFinding = new JComboBox();
	
	// - JPanel
	private JPanel jpNorth = new JPanel();
	private JPanel jpSouth = new JPanel();
	private JPanel jpCenter = new JPanel();
	private JPanel jpWest = new JPanel();
	private JPanel jpCenterGeneral = new JPanel();
	private JPanel jpCenterPheromone= new JPanel();
	private JPanel jpCenterPheromoneComposants= new JPanel();
	private JPanel jpCenterFourmis = new JPanel();
	private JPanel jpCenterFourmisComposants = new JPanel();
	
	private JPanel jpPheromonePortee = new JPanel();
	private JPanel jpPheromonePersistance = new JPanel();
	private JPanel jpFourmisTailleFourmiliere = new JPanel();
	private JPanel jpFourmisDureeVie = new JPanel();
	private JPanel jpFourmisNbDepart = new JPanel();
	private JPanel jpFourmisFrequencePonte = new JPanel();
	private JPanel jpFourmisNourritureDepart = new JPanel();
	
	// - Slider
	static final int PHEROMONE_PORTEE_MIN = 0;
	static final int PHEROMONE_PORTEE_MAX = 10;
	JSlider jsPheromonesPortee = new JSlider(JSlider.HORIZONTAL, PHEROMONE_PORTEE_MIN, PHEROMONE_PORTEE_MAX, ((PHEROMONE_PORTEE_MAX - PHEROMONE_PORTEE_MIN) / 2));
	
	static final int PHEROMONE_PERSISTANCE_MIN = 0;
	static final int PHEROMONE_PERSISTANCE_MAX = 10;
	JSlider jsPheromonesPersistance = new JSlider(JSlider.HORIZONTAL, PHEROMONE_PERSISTANCE_MIN, PHEROMONE_PERSISTANCE_MAX, ((PHEROMONE_PERSISTANCE_MAX - PHEROMONE_PERSISTANCE_MIN) / 2));
	
	static final int FOURMIS_TAILLE_FOURMILLIERE_MIN = 0;
	static final int FOURMIS_TAILLE_FOURMILLIERE_MAX = 10;
	JSlider jsFourmisTailleFourmilliere = new JSlider(JSlider.HORIZONTAL, FOURMIS_TAILLE_FOURMILLIERE_MIN, FOURMIS_TAILLE_FOURMILLIERE_MAX, ((FOURMIS_TAILLE_FOURMILLIERE_MAX - FOURMIS_TAILLE_FOURMILLIERE_MIN) / 2));
	
	static final int FOURMIS_DUREE_VIE_MIN = 0;
	static final int FOURMIS_DUREE_VIE_MAX = 10;
	JSlider jsFourmisDureeVie = new JSlider(JSlider.HORIZONTAL, FOURMIS_DUREE_VIE_MIN, FOURMIS_DUREE_VIE_MAX, ((FOURMIS_DUREE_VIE_MAX - FOURMIS_DUREE_VIE_MIN) / 2));
	
	static final int FOURMIS_NB_DEPART_MIN = 0;
	static final int FOURMIS_NB_DEPART_MAX = 200;
	JSlider jsFourmisNbDepart = new JSlider(JSlider.HORIZONTAL, FOURMIS_NB_DEPART_MIN, FOURMIS_NB_DEPART_MAX, ((FOURMIS_NB_DEPART_MAX - FOURMIS_NB_DEPART_MIN) / 2));
	
	static final int FOURMIS_FREQUENCE_PONTE_MIN = 0;
	static final int FOURMIS_FREQUENCE_PONTE_MAX = 10;
	JSlider jsFourmisFrequencePonte = new JSlider(JSlider.HORIZONTAL, FOURMIS_FREQUENCE_PONTE_MIN, FOURMIS_FREQUENCE_PONTE_MAX, ((FOURMIS_FREQUENCE_PONTE_MAX - FOURMIS_FREQUENCE_PONTE_MIN) / 2));
	
	static final int FOURMIS_NOURRITURE_DEPART_MIN = 0;
	static final int FOURMIS_NOURRITURE_DEPART_MAX = 5000;
	JSlider jsFourmisNourritureDepart= new JSlider(JSlider.HORIZONTAL, FOURMIS_NOURRITURE_DEPART_MIN, FOURMIS_NOURRITURE_DEPART_MAX, ((FOURMIS_NOURRITURE_DEPART_MAX - FOURMIS_NOURRITURE_DEPART_MIN) / 2));

	private final Carte carte;
	
	private Fourmiliere currentFourmiliere;
	
	public ParamView(Carte applicationController) 
	{
		
		
		
		this.carte = applicationController;
		Dimension screenSize = java.awt.Toolkit.getDefaultToolkit().getScreenSize();
	    Integer widthFen = 750;
	    Integer heightFen = 430;
		
	    fenParam.setLayout(new BorderLayout());
		
		// NORTH
		jpNorth.add(jlTitre);
		
		fenParam.add(jpNorth, BorderLayout.NORTH);
		
		// WEST
		final JTable jtFourmiliere = new JTable(new FourmilliereTableModel(this.carte));
		
		// Gestion du click d4une cellule
		ListSelectionModel cellSelectionModel = jtFourmiliere.getSelectionModel();
		cellSelectionModel.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
		cellSelectionModel.addListSelectionListener(new ListSelectionListener() {	
			public void valueChanged(ListSelectionEvent e) {
				String selectedData = null;
				
				int[] selectedRow = jtFourmiliere.getSelectedRows();

				int fourmId = (int)jtFourmiliere.getValueAt(selectedRow[0], 1);
				String fourmNom = (String)jtFourmiliere.getValueAt(selectedRow[0], 0);
				
								
				Fourmiliere fourmilliereSelectionnee = carte.getFourmilliereWithId(fourmId);
				
				currentFourmiliere = fourmilliereSelectionnee;
				
				jtNom.setText(fourmilliereSelectionnee.getNom());
				jsFourmisNbDepart.setValue(fourmilliereSelectionnee.getNbDepart());
				jsFourmisNourritureDepart.setValue(fourmilliereSelectionnee.getNourriture());
				jsFourmisDureeVie.setValue(fourmilliereSelectionnee.getDureeVie());
				
			}

	    });
		jtFourmiliere.setAutoResizeMode(JTable.AUTO_RESIZE_ALL_COLUMNS);
		
		JScrollPane js = new JScrollPane(jtFourmiliere);
		js.setPreferredSize(new Dimension(100, 200));
		
		jpWest.add(js);
		
		JPanel jp = new JPanel();
		jp.add(jpWest);
		
		fenParam.add(jp, BorderLayout.WEST);
		
		// CENTER
		// - Général
		jpCenterGeneral.setLayout(new FlowLayout());
		jpCenterGeneral.add(jlCenterGeneralNom);
		jpCenterGeneral.add(jtNom);
		jpCenterGeneral.add(jlCenterGeneralPathFinding);
		jpCenterGeneral.add(jcPathFinding);
		
		// - Phéromones
		jpCenterPheromoneComposants.setBorder(BorderFactory.createTitledBorder(PHEROMONES));
		jpCenterPheromoneComposants.setLayout(new GridLayout(1, 3));
		
		// - - Portee
		jtPheromonePortee.setText(String.valueOf(jsPheromonesPortee.getValue()));
		jsPheromonesPortee.addChangeListener(new ChangeListener() {
			public void stateChanged(ChangeEvent e) {
				jtPheromonePortee.setText(String.valueOf(jsPheromonesPortee.getValue()));
	        }
		});
//		jpCenterPheromoneComposants.add(jlCenterPheromonesPortee);
//		jpCenterPheromoneComposants.add(jsPheromonesPortee);
//		jpPheromonePortee.add(jtPheromonePortee);
//		jpCenterPheromoneComposants.add(jpPheromonePortee);
		
		// - - Persistance
		jtPheromonePersistance.setText(String.valueOf(jsPheromonesPersistance.getValue()));
		jsPheromonesPersistance.addChangeListener(new ChangeListener() {
			public void stateChanged(ChangeEvent e) {
				jtPheromonePersistance.setText(String.valueOf(jsPheromonesPersistance.getValue()));
	        }
		});
		jpCenterPheromoneComposants.add(jlCenterPheromonesPersistance);
		jpCenterPheromoneComposants.add(jsPheromonesPersistance);
		jpPheromonePersistance.add(jtPheromonePersistance);
		jpCenterPheromoneComposants.add(jpPheromonePersistance);
		
		jpCenterPheromone.add(jpCenterPheromoneComposants);
		
		// - Fourmis
		jpCenterFourmisComposants.setBorder(BorderFactory.createTitledBorder(FOURMIS));
		jpCenterFourmisComposants.setLayout(new GridLayout(3, 3));
		
		jtFourmisTailleFourmiliere.setText(String.valueOf(jsFourmisTailleFourmilliere.getValue()));
		jsFourmisTailleFourmilliere.addChangeListener(new ChangeListener() {
			public void stateChanged(ChangeEvent e) {
				jtFourmisTailleFourmiliere.setText(String.valueOf(jsFourmisTailleFourmilliere.getValue()));
	        }
		});
//		jpCenterFourmisComposants.add(jlCenterFourmisTailleFourmillieres);
//		jpCenterFourmisComposants.add(jsFourmisTailleFourmilliere);
//		jpFourmisTailleFourmiliere.add(jtFourmisTailleFourmiliere);
//		jpCenterFourmisComposants.add(jpFourmisTailleFourmiliere);

		jtFourmisDureeVie.setText(String.valueOf(jsFourmisDureeVie.getValue()));
		jsFourmisDureeVie.addChangeListener(new ChangeListener() {
			public void stateChanged(ChangeEvent e) {
				jtFourmisDureeVie.setText(String.valueOf(jsFourmisDureeVie.getValue()));
	        }
		});
		jpCenterFourmisComposants.add(jlCenterFourmisDureeVie);
		jpCenterFourmisComposants.add(jsFourmisDureeVie);
		jpFourmisDureeVie.add(jtFourmisDureeVie);
		jpCenterFourmisComposants.add(jpFourmisDureeVie);

		jtFourmisNbDepart.setText(String.valueOf(jsFourmisNbDepart.getValue()));
		jsFourmisNbDepart.addChangeListener(new ChangeListener() {
			public void stateChanged(ChangeEvent e) {
				jtFourmisNbDepart.setText(String.valueOf(jsFourmisNbDepart.getValue()));
	        }
		});
		jpCenterFourmisComposants.add(jlCenterFourmisNbDepart);
		jpCenterFourmisComposants.add(jsFourmisNbDepart);
		jpFourmisNbDepart.add(jtFourmisNbDepart);
		jpCenterFourmisComposants.add(jpFourmisNbDepart);

		jtFourmisFrequencePonte.setText(String.valueOf(jsFourmisFrequencePonte.getValue()));
		jsFourmisFrequencePonte.addChangeListener(new ChangeListener() {
			public void stateChanged(ChangeEvent e) {
				jtFourmisFrequencePonte.setText(String.valueOf(jsFourmisFrequencePonte.getValue()));
	        }
		});
//		jpCenterFourmisComposants.add(jlCenterFourmisFrequencePonte);
//		jpCenterFourmisComposants.add(jsFourmisFrequencePonte);
//		jpFourmisFrequencePonte.add(jtFourmisFrequencePonte);
//		jpCenterFourmisComposants.add(jpFourmisFrequencePonte);

		jtFourmisNourritureDepart.setText(String.valueOf(jsFourmisNourritureDepart.getValue()));
		jsFourmisNourritureDepart.addChangeListener(new ChangeListener() {
			public void stateChanged(ChangeEvent e) {
				jtFourmisNourritureDepart.setText(String.valueOf(jsFourmisNourritureDepart.getValue()));
	        }
		});
		jpCenterFourmisComposants.add(jlCenterFourmisNourritureDepart);
		jpCenterFourmisComposants.add(jsFourmisNourritureDepart);
		jpFourmisNourritureDepart.add(jtFourmisNourritureDepart);
		jpCenterFourmisComposants.add(jpFourmisNourritureDepart);
		
		jpCenterFourmis.add(jpCenterFourmisComposants);
		
		// 
		jpCenter.add(jpCenterGeneral);
		jpCenter.add(jpCenterPheromone);
		jpCenter.add(jpCenterFourmis);
		
		fenParam.add(jpCenter);
		
		// SOUTH
		jbAnnuler.addActionListener(this);
		jbAppliquer.addActionListener(this);
		jpSouth.add(jbAnnuler);
		jpSouth.add(jbAppliquer);
		
		fenParam.add(jpSouth, BorderLayout.SOUTH);
		
		// Param�trage fen�tre : taille, position et visibilit�
		fenParam.setSize(widthFen, heightFen);
	    
	    fenParam.setLocation((int)((screenSize.getWidth()-widthFen)/2), 
	        (int)((screenSize.getHeight()-heightFen)/2));
		
		fenParam.setVisible(true);
		fenParam.setResizable(false);
		
	}

	@Override
	public void actionPerformed(ActionEvent arg0) 
	{
		// TODO Auto-generated method stub
		if(arg0.getSource() == jbAnnuler) {
			fenParam.setVisible(false);
			fenParam.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		} else if(arg0.getSource() == jbAppliquer) {
			// On sauvegarde les changements
			if (this.currentFourmiliere != null) {
				this.currentFourmiliere.setNom(jtNom.getText());
				this.currentFourmiliere.setNbDepart(Integer.valueOf(jtFourmisNbDepart.getText()));
				this.currentFourmiliere.setDureeVie(Integer.valueOf(jtFourmisDureeVie.getText()));
				this.currentFourmiliere.setNourriture(Integer.valueOf(jtFourmisNourritureDepart.getText()));
				JOptionPane.showMessageDialog(fenParam, "La fourmillière " + this.currentFourmiliere.getNom() + " à bien été modifiée !");
			}
			
			//fenParam.setVisible(false);
			//fenParam.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		}
	}
}
